package cn.tedu.csmall.commons.restful;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

//通用的支持分页查询返回的类
@Data
@Accessors(chain = true)
public class JsonPage<T> implements Serializable {

    @ApiModelProperty(value = "当前页码", name = "pageNum")
    private Integer pageNum;

    @ApiModelProperty(value = "每页条数", name = "pageSize")

    private Integer pageSize;

    @ApiModelProperty(value = "总条数", name = "totalCount")
    private Long totalCount;
    @ApiModelProperty(value = "总页数", name = "totalPages")
    private Integer totalPages;

    //查询出所有对象集合
    @ApiModelProperty(value = "分页数据", name = "list")
    private List<T> list;

    //定义一个方法,实现将PageInfo类型中对象的值赋值给当前JSONPage对象对应的属性
    //相当于一个转换方法

    public static <T> JsonPage<T> restPage(PageInfo<T> pageInfo) {

        JsonPage<T> result = new JsonPage<>();

        result.setPageNum(pageInfo.getPageNum())
                .setPageSize(pageInfo.getPageSize())
                .setTotalCount(pageInfo.getTotal())
                .setTotalPages(pageInfo.getPages())

                .setList(pageInfo.getList());

        return result;

    }


}
